package com.live.dengdengtv.activity.my;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.live.dengdengtv.BaseActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.LoginInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.bean.gson.GUserInfo;
import com.live.dengdengtv.net.NetTaskList;
import com.live.dengdengtv.utils.Notice;

import java.util.HashMap;

/**
 * Created by leiyun on 2016/8/11.
 */
public class MyProfitsActivity extends BaseActivity implements View.OnClickListener{

    TextView receive_record_tv;
    private long virTicket = 0;
    private TextView virtual_ticket_tv;
    private TextView limit_virCoin_tv;
    private TextView limit_money_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_profits);
        receive_record_tv=(TextView)findViewById(R.id.receive_record_tv);
        receive_record_tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//文字下划线


        virtual_ticket_tv = (TextView)findViewById(R.id.virtual_ticket_tv);
        limit_virCoin_tv = (TextView)findViewById(R.id.limit_virCoin_tv);
        limit_money_tv = (TextView)findViewById(R.id.limit_money_tv);

//        setData();

        findViewById(R.id.exchange_vircoin_bt).setOnClickListener(this);

        getMyInfo();//刷新账户金币信息
    }

    private void setData() {
        try {
            virTicket = Long.parseLong(LoginInfo.getInstance().getgUserInfo().virtual_ticket);
        }catch (Exception e){
            e.printStackTrace();
        }
        virtual_ticket_tv.setText(virTicket+"张");

        limit_virCoin_tv.setText((virTicket*10/12)+"虚拟币");//虚拟币：虚拟礼券＝10:12
        limit_money_tv.setText((virTicket/26)+"元");//现金：虚拟礼券＝1:26
    }


    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.receive_record_tv){
            Intent intent = new Intent();
            intent.setClass(this, ReceiveRecordActivity.class);
            startActivityForResult(intent, 100);
        }else if(v.getId() == R.id.exchange_vircoin_bt){
            if(virTicket!=0 ){
                exchangeVirTicket();
            }
        }
    }

    private void exchangeVirTicket(){
        HashMap<String,String> params = new HashMap<String, String>();
        params.put("exchange_type","1");//兑换类型：1、兑换虚拟币；2、兑换现金；
        params.put("virtual_ticket_count","12");//兑换使用的虚拟礼券数量；TODO
        params.putAll(AppInfo.getHeadInfo());
        requestData(NetTaskList.exchange, params,1);
    }
    private void getMyInfo(){

        requestData(NetTaskList.get_userInfo, AppInfo.getHeadInfo(),2);
    }

    @Override
    public boolean onResponse(SimpleInfoData response, int id) {

        if (response.result == 0) {
            if (id == 1) {
                Notice.alert(context, "已兑换" + virTicket + "为虚拟币");
                getMyInfo();//刷新虚拟币信息
            }else if(id == 2){
                GUserInfo userInfo = new Gson().fromJson(response.data.getAsJsonObject(), GUserInfo.class);
                LoginInfo.getInstance().setgUserInfo(userInfo);
                setData();
            }
        } else {
            Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
        }

        return false;
    }
}
